package vn.conghuy.talkwithme;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class TemplateActivity extends ExpandableListActivity {
	
	SimpleExpandableListAdapter listAdapter;
	ArrayList<String[]> lSubItems;

    @SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);
        
        listAdapter = new SimpleExpandableListAdapter(this, createGroupList(), R.layout.group_row,
        		new String[]{"Group Item"}, new int[]{R.id.row_name}, 
        		createChildList(), R.layout.child_row, new String[]{"Child Item"}, new int[]{R.id.grp_child});
        setListAdapter(listAdapter);
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
	List createGroupList(){
    	String[] groups = getResources().getStringArray(R.array.group_name_template);
    	ArrayList result = new ArrayList();
    	for(int i = 0; i < groups.length; i++){
    		HashMap m =new HashMap();
    		m.put("Group Item", groups[i]);
    		result.add(m);
    	}
    	return (List)result;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	List createChildList(){
    	ArrayList result = new ArrayList();
    	
    	// add new group here
    	lSubItems = new ArrayList<String[]>();
    	lSubItems.add(getResources().getStringArray(R.array.sms_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.call_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.calendar_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.email_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.map_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.launch_app_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.search_information_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.facebook_child_name_template));
    	lSubItems.add(getResources().getStringArray(R.array.football_child_name_template));
    	
    	for(int i = 0; i< lSubItems.size(); i++){
    		String[] items = lSubItems.get(i);
        	ArrayList itemList = new ArrayList();
        	for(int j = 0; j < items.length; j++){
        		HashMap m = new HashMap();
        		m.put("Child Item", items[j]);
        		itemList.add(m);
        	}
        	result.add(itemList);
    	}
    	
    	return (List)result;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_template, menu);
        return true;
    }
    
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
    		int groupPosition, int childPosition, long id) {
    	Intent intent = new Intent();
    	intent.putExtra("text", lSubItems.get(groupPosition)[childPosition]);
    	setResult(RESULT_OK, intent);
    	finish();
    	return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
}
